<?php
/**
 * Created by PhpStorm.
 * Author: kaodou
 * Date: 2017/12/12
 * Time: 14:00
 */

namespace App\Http\Admin\Supplier\Controllers;

use Framework\BaseClass\Http\Admin\Controller;
use App\Http\Admin\Website\Models\Menu;
use App\Eloquent\Ygt\Supplier;
use App\Engine\Supplier as SupplierEngine;
use App\Engine\Func;
use App\Eloquent\Province;
use App\Eloquent\City;
use App\Eloquent\Area;

class IndexController extends Controller
{
    protected $modelName = 'Supplier';
    protected $company = true;

    protected $rules = [
        'supplier_name' => 'bail|required',
        'sort_id' => 'required',
    ];

    public function __construct()
    {
        parent::__construct();
        $this->model = new Supplier();

    }

    protected function _vdataAdd(){
        $levels = SupplierEngine::getAllLevel();
        $provinceList           = Province::all();
        return ['levels'=>$levels,'provinceList'=>$provinceList];
    }

    protected function _where(){

        $where = [];
        if($supplierName = request('supplier_name')){
            $where['supplier_name'] = ['like','%'.$supplierName.'%'];
        }
        return $where;
    }

    protected function _hdata($result){
        $result->transform(function ($item, $key) {
            $item->avatars_url = Func::getImgUrlById($item->avatars_id);
            $item->supplier_level_title = SupplierEngine::getLevelTitle($item->supplier_level);
            $item->address = $item->province_name.'-'.$item->city_name.'-'.$item->area_name;
            return $item;
        });
        return $result;
    }

    protected function _honedata($info){
        $info->avatars_url = Func::getImgUrlById($info->avatars_id);
        return $info;
    }

    protected function _export_title(){
        return [
            'supplier_name'=>'供应商名称',
            'sort_id'=>'排序',
        ];
    }

    protected function _cdata(){
        $provinceInfo = Province::where('provinceID',request('province_id'))->first();
        $cityInfo = City::where('cityID',request('city_id'))->first();
        $areaInfo = Area::where('areaID',request('area_id'))->first();
        $pinyin = app('pinyin');
        $firstLetter = strtoupper(substr($pinyin->abbr(request('customer_name')),0,1));
        return [
            'province_name' => isset($provinceInfo->province)?$provinceInfo->province:'',
            'city_name' => isset($cityInfo->city)?$cityInfo->city:'',
            'area_name' => isset($areaInfo->area)?$areaInfo->area:'',
            'first_letter'=>$firstLetter,
        ];
    }

}